export const isObject=item=>Object.prototype.toString.call(item)==='[object Object]'
export const isArray=item=>Object.prototype.toString.call(item)==='[object Array]'
const routers = ['switchTab', 'reLaunch', 'redirectTo', 'navigateTo', 'navigateBack']
export default class AniRouter {
	constructor() {
		this.routerConfig=[]
		this.proxyRouter()
	}
	setRouterIntercept(data){
		if(isArray(data)){
			this.routerConfig=this.routerConfig.concat(data)
		}else if(isObject(data)){
			this.routerConfig.push(data)
		}
	}

	setRouterListen(data){
		if(!isObject(data)) throw new Error('类型错误')
		this.routerConfig.push(data)
	}

	proxyRouter() {
		let that = this
		let vm={};
		Object.entries(wx).forEach(([key,val])=>{
			if(routers.includes(key)){
				let m=val
				vm[key]=function (data){
					if(data.proxy===false) return m(data)
					const router={}
					router.options={}
					const parameter=data.url.split('?')
					router.path=parameter[0]
					if(parameter[1]){
						const optionsArr=parameter[1].split('&').map(x=>x.split('='))
						const options={}
						optionsArr.forEach(([key,val])=>options[key]=val)
						router.options=options
					}
					const Page=getCurrentPages();
					const currPage=Page[Page.length-1]
					if(currPage.$beforeRouter){
						let pageBeforeFn = currPage.$beforeRouter({data, routerType:key,beforePage:currPage})
						if (pageBeforeFn === false) return false
						if (isObject(pageBeforeFn)) {
							if (pageBeforeFn.data) data = pageBeforeFn.data
							if (pageBeforeFn.routerType && routers.includes(pageBeforeFn.routerType)) {
								m= wx[pageBeforeFn.routerType]
							}
						}
					}

					that.routerConfig.forEach( (config)=>{

						if(config.url!=undefined) {
							if(typeof config.url==="string"){
								if(data.url.indexOf(config.url)===-1) return
							}else if(isArray(config.url)){
								if(config.url.every(x=>data.url.indexOf(x)===-1)) return
							}else {
								throw new Error(`${config.url},url规则类型错误`)
							}
						}

						if (config.after) {
							data.success = function () {
								setTimeout(_ => {
									let currPage = getCurrentPages()[getCurrentPages().length - 1]
									if (config.after) config.after.call(currPage, data)
								}, 300)
							}
						}

						if (config.before) {
							let beforeFn = config.before.call(currPage,{ routerType:key,router})
							if (beforeFn === false) return false

							if (isObject(beforeFn)) {
								if (beforeFn.router) {
									let {path,options}=beforeFn.router
									if(options){
										const optionsArr=Object.entries(options)
										if(optionsArr.length){
											path=path+'?'+optionsArr.map(x=>x.join('=')).join('&')
										}
									}
									console.log(data)
									data.url=path
									// debugger
								}

								// if (beforeFn.routerType && routers.includes(beforeFn.routerType)&&beforeFn.routerType!=data.routerType) {
								// 	m = wx[beforeFn.routerType]
								// }
							}

						}
					})

					m(data)
				}
			}else{
				vm[key]=val
			}
		})
		wx=vm
	}
}